package com.p.lib.common.rx;

import com.p.lib.common.exception.ApiException;
import com.p.lib.entity.ObjectResponse;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/5/31 0031.
 */

public class RxHttpResponseCompat {


    /**
     * 预处理， 响应成功，将data单独拆分出来
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<ObjectResponse<T>, T> compatResult(){

        return new ObservableTransformer<ObjectResponse<T>, T>() {
            @Override
            public ObservableSource<T> apply(Observable<ObjectResponse<T>> upstream) {
                return upstream.flatMap(new Function<ObjectResponse<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(final ObjectResponse<T> tObjectResponse) throws Exception {

                        if(tObjectResponse.isSuccess()){

                            return Observable.create(new ObservableOnSubscribe<T>() {
                                @Override
                                public void subscribe(ObservableEmitter<T> emitter) throws Exception {

                                    try{
                                        if(tObjectResponse.data != null){
                                            emitter.onNext(tObjectResponse.data);
                                        }
                                        emitter.onComplete();
                                    }catch (Exception e){
                                        emitter.onError(e);
                                    }

                                }
                            });

                        }else{
                            return Observable.error(new ApiException(tObjectResponse.getCode(), tObjectResponse.getMessage()));
                        }
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * 预处理， 响应成功，没有Data的情况
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<ObjectResponse<T>, ?> compatResultObject(){

        return new ObservableTransformer<ObjectResponse<T>, Object>() {
            @Override
            public ObservableSource<Object> apply(Observable<ObjectResponse<T>> upstream) {
                return upstream.flatMap(new Function<ObjectResponse<T>, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(final ObjectResponse<T> tObjectResponse) throws Exception {

                        if(tObjectResponse.isSuccess()){

                            return Observable.just(1);

                        }else{
                            return Observable.error(new ApiException(tObjectResponse.getCode(), tObjectResponse.getMessage()));
                        }
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

}
